package org.bigjava.controller;

import org.bigjava.bean.Garbage;
import org.bigjava.bean.Xiaoqu;
import org.bigjava.biz.GarbageService;
import org.bigjava.biz.XiaoquService;
import org.bigjava.common.Constant;
import org.bigjava.common.FenYe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class GarbageController {
    @Autowired
    HttpSession session;
    @Autowired
    GarbageService garbageService;
    @Autowired
    XiaoquService xiaoquService;

    //根据垃圾站点编号或者垃圾站点名称搜索
    @RequestMapping("garbage/sousuo")
    public String garbageSouSuo(String dyg,String deg){
        if(dyg==""){dyg = null;}
        if(deg==""){deg = null;}
        List<Garbage> lists = garbageService.garbageMoHu(dyg,deg);
        session.setAttribute("garbages",lists);
        return "garbagegl";
    }
    //删除垃圾站点
    @RequestMapping("garbage/shanchu")
    public String garbageShanChu(int id){
        garbageService.garbageShanChu(id);
        Object a = session.getAttribute("yeshu");
        return "redirect:/garbage/fenye?a="+a;
    }
    //修改垃圾站点
    @RequestMapping("garbage/xiugai")
    public String garbageXiuGai(Garbage garbage){
        Garbage garbagea = (Garbage) session.getAttribute("garbage");
        garbage.setId(garbagea.getId());
        garbageService.garbageXiuGai(garbage);
        return "redirect:/garbage/fenye";
    }
    //跳转到修改垃圾站点信息页面
    @RequestMapping("garbage/xg")
    public String garbagexg(int id){
        Garbage garbage = garbageService.garbageId(id);
        session.setAttribute("garbage",garbage);
        List<Xiaoqu> xiaoqus = xiaoquService.xiaoQuQuan();
        session.setAttribute("xiaoqus",xiaoqus);
        return "garbagexg";
    }
    //跳转到添加垃圾站点页面
    @RequestMapping("garbage/garbagetj")
    public String garbagetj(){
        List<Xiaoqu> xiaoqus = xiaoquService.xiaoQuQuan();
        session.setAttribute("xiaoqus",xiaoqus);
        return "garbagetj";
    }
    //添加垃圾站点信息
    @RequestMapping("garbage/tianjia")
    public String garbageAdd(Garbage garbage){
        garbageService.garbageAdd(garbage);
        return "redirect:/garbage/fenye";
    }
    //分页查询站点信息
    @RequestMapping("garbage/fenye")
    public String garbageFenYe(String a){
        //业主总人数
        int zong =  garbageService.garbageZong();
        session.setAttribute("zong",zong);
        //调用分页方法返回下一页开始id
        int ks = FenYe.fenYe(session,zong,a, Constant.fenye);
        //分页查询出的业主
        List<Garbage> lists = garbageService.garbageFenYe(ks,Constant.fenye);
        session.setAttribute("garbages",lists);
        return "garbagegl";
    }
}
